#!/usr/bin/env python
# encoding: utf-8
"""
#-------------------------------------------------------------------#
#                   CONFIDENTIAL --- CUSTOM STUDIOS                 #     
#-------------------------------------------------------------------#
#                                                                   #
#                   @Project Name : Globallawonline                #
#                                                                   #
#                   @File Name    : DFAFilter.py                      #
#                                                                   #
#                   @Programmer   : 李建                            #
#                                                                   #  
#                   @Start Date   : 2021/5/27 9:40                 #
#                                                                   #
#                   @Last Update  : 2021/5/27 9:40                 #
#                                                                   #
#-------------------------------------------------------------------#
# Classes:DFA敏感词过滤算法                                                          #
#                                                                   #
#-------------------------------------------------------------------#
"""
import time


# DFA算法
class DFAFilter():
    def __init__(self):
        self.keyword_chains = {}
        self.delimit = '\x00'

    def add(self, keyword):
        """
        将敏感词加入敏感词树
        :param keyword:敏感词
        :return:
        """
        keyword = keyword.lower()   # 敏感词英文变为小写
        chars = keyword.strip()     # 敏感词去除首尾空格和换行
        if not chars:   # 如果敏感词为空直接返回
            return
        level = self.keyword_chains
        # 遍历敏感词的每个字
        for i in range(len(chars)):
            # 如果这个字已经存在字符链的key中就进入其子字典
            if chars[i] in level:
                level = level[chars[i]]
            else:
                if not isinstance(level, dict):
                    break
                for j in range(i, len(chars)):
                    level[chars[j]] = {}
                    last_level, last_char = level, chars[j]
                    level = level[chars[j]]
                last_level[last_char] = {self.delimit: 0}
                break
        if i == len(chars) - 1:
            level[self.delimit] = 0

    def parse(self, path):
        """
        处理敏感词文本
        :param path:敏感词文本路径
        :return:将敏感词表生成敏感词树
        """
        with open(path,encoding='utf-8') as f:
            for keyword in f:
                self.add(str(keyword).strip())

    def filter(self, message):
        """
        判断目标文本中的是否有敏感词
        :param message:目标文本
        :return:strl/False 敏感词/无
        """
        lmessage = message.lower()   # 文本小写
        start = 0
        while start < len(message):
            level = self.keyword_chains
            step_ins = 0
            for char in lmessage[start:]:
                if char in level:
                    step_ins += 1
                    if self.delimit not in level[char]:
                        level = level[char]
                    else:
                        strl = lmessage[start:start + step_ins - 2]
                        start += step_ins - 1
                        return strl
                else:
                    break
            start += 1
        return False

        # ret = []    # 返回的文本
        # start = 0
        # while start < len(message):
        #     level = self.keyword_chains
        #     step_ins = 0
        #     for char in lmessage[start:]:
        #         if char in level:
        #             step_ins += 1
        #             if self.delimit not in level[char]:
        #                 level = level[char]
        #             else:
        #                 ret.append(repl * step_ins)
        #                 start += step_ins - 1
        #                 break
        #         else:
        #             ret.append(message[start])
        #             break
        #     else:
        #         ret.append(message[start])
        #     start += 1
        #
        # return ''.join(ret)


if __name__ == "__main__":
    time1 = time.time()
    gfw = DFAFilter()
    path="反动词库.txt"
    gfw.parse(path)
    text="<p>Dalam sumpahnya, empat puluh Pejabat Fungsional yang dilantik berjanji akan bekerja sebaik-baiknya dan penuh tanggung jawab. Mereka juga berjanji akan menjaga integritas dan menghindarkan diri dari perbuatan tercela.</p>"
    result = gfw.filter(text)

    print(text)
    print(result)
    time2 = time.time()
    print('总共耗时：' + str(time2 - time1) + 's')
